/*
 *精准投放
 * @Author: Wilbur
 * @Date: 2018-11-21 09:14:02
 * @Last Modified by: Wilbur
 * @Last Modified time: 2018-12-08 14:31:04
 */
<template>
    <div class="container precision-list-container">
        <div class="operation-wrapper">
            <el-tooltip
                class="item"
                effect="dark"
                content="点击保存并发送通知会自动把任务上架"
                placement="top">
                <el-button
                    type="primary"
                    @click="fnSendMsgToSelectedClass"
                    size="small">上架并发送通知</el-button>
            </el-tooltip>
        </div>
        <div class="set-status-wrapper">
            <span>投放状态：</span>
            <el-switch
                v-model="class_limit"
                @change="fnSetClassOpenStatus"
                active-text="开放"
                inactive-text="封闭"/>

            <p
                style="display: inline-block;margin-left: 15px;color: #999999;"
                v-if="class_limit">*指定的班级界面，会出现任务，但是其他班级依然能在赞助大厅找到</p>
            <p
                style="display: inline-block;margin-left: 15px;color: #999999;"
                v-else>*非指定的班级，在哪里都找不到这个任务，并且不能做。默认是封闭</p>
        </div>
        <div class="content-wrapper">
            <el-row
                class="tab-switch-wrapper"
                :gutter="20">
                <el-col
                    class="filter-item"
                    :md="{ span: 24 }"
                    :sm="{ span: 24 }">
                    <el-radio-group
                        @change="fnChangeSelectStatus"
                        v-model="classSelected">
                        <el-radio-button
                            :label="0">未选
                        </el-radio-button>
                        <el-radio-button
                            :label="1">已选
                        </el-radio-button>
                    </el-radio-group>
                </el-col>
            </el-row>
            <el-row
                v-if="classSelected == 0"
                class="search-wrapper"
                :gutter="20">
                <el-form
                    label-position="left"
                    label-width="84px"
                    size="small">
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="激活状态：">
                            <el-select
                                v-model="filterData1.class_enable"
                                filterable
                                @change="getTableData(1)"
                                placeholder="请选择">
                                <el-option
                                    v-for="item in activeStatus"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="选择城市：">
                            <el-select
                                v-model="filterData1.city_id"
                                filterable
                                @change="fnCityChange"
                                placeholder="请选择">
                                <el-option
                                    v-for="item in cityList"
                                    :key="item.city_id"
                                    :label="item.name"
                                    :value="item.city_id"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="选择学校：">
                            <el-select
                                v-model="filterData1.school_id"
                                filterable
                                @change="fnSchoolChange"
                                placeholder="请选择">
                                <el-option
                                    v-for="item in schoolList"
                                    :key="item.school_id"
                                    :label="item.name"
                                    :value="item.school_id"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="选择学院：">
                            <el-select
                                v-model="filterData1.academy_no"
                                filterable
                                @change="fnAcademyChange"
                                placeholder="请选择">
                                <el-option
                                    v-for="item in academyList"
                                    :key="item.academy_no"
                                    :label="item.name"
                                    :value="item.academy_no"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="选择年级：">
                            <el-select
                                v-model="filterData1.grade"
                                filterable
                                @change="getTableData(1)"
                                placeholder="请选择年级">
                                <el-option
                                    v-for="item in gradeList"
                                    :key="item.grade"
                                    :label="item.label"
                                    :value="item.grade" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="search-btn-wrapper"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-button
                            size="small"
                            type="default"
                            @click="fnReset">重置</el-button>
                    </el-col>
                </el-form>
            </el-row>
            <div class="table-wrapper">
                <el-table
                    :data="tableData1"
                    ref="multipleTable"
                    @select="fnSelect"
                    @select-all="fnSelectAllTop"
                    @selection-change="handleSelectionChange"
                    height="100%"
                    style="width: 100%;">
                    <el-table-column
                        align="center"
                        type="selection"/>
                    <el-table-column
                        fixed="left"
                        prop="school_name"
                        align="center"
                        label="学校名称"/>
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="academy_name"
                        show-overflow-tooltip
                        align="center"
                        label="学院名称" />
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="grade"
                        align="center"
                        label="年级" />
                    <el-table-column
                        prop="class_name"
                        align="center"
                        label="班级名称" />
                    <el-table-column
                        prop="student_count"
                        align="center"
                        label="入驻人数/班级人数">
                        <template slot-scope="scope">
                            {{ scope.row.now_student_count }}/{{ scope.row.student_count }}
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="money"
                        label="班级状态">
                        <template slot-scope="scope">
                            <template v-if="scope.row.enable == 1">
                                <span class="class-status completed">已激活</span>
                            </template>
                            <template v-else>
                                <span class="class-status wait-audit">未激活</span>
                            </template>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="has_send_message"
                        label="通知">
                        <template slot-scope="scope">
                            <span v-if="scope.row.has_send_message == 1">已发送</span>
                            <span v-else>未发送</span>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination-wrapper clearfix">
                <!-- tab 已选 展示删除按钮 -->
                <div
                    class="select-all"
                    v-if="classSelected == 1">
                    <el-checkbox
                        v-model="checkedAll"
                        :indeterminate="checkIndeterminate"
                        @change="fnChangeChecked">全选</el-checkbox>
                    <el-button
                        @click="fnRemoveFromSelectedList"
                        class="del-btn"
                        size="small">
                        <span v-if="multipleSelection.length == tableData1.length">全部删除</span>
                        <span v-else>删除所选</span>
                    </el-button>
                </div>
                <!-- tab 未选 展示加入按钮 -->
                <div
                    class="select-all"
                    v-if="classSelected == 0">
                    <el-checkbox
                        v-model="checkedAll"
                        :indeterminate="checkIndeterminate"
                        @change="fnChangeChecked">全选</el-checkbox>
                    <el-button
                        class="del-btn"
                        @click="fnAddToSelectedList"
                        size="small">
                        <span v-if="multipleSelection.length == tableData1.length">全部加入</span>
                        <span v-else>加入所选</span>
                    </el-button>
                </div>
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData" />
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            checkIndeterminate: false,
            multipleSelection: [],
            checkedAll: false,
            classSelected: 0,
            class_limit: false,
            // 未选择列表筛选项
            filterData1: {
                city_id: '',
                school_id: '',
                academy_no: '',
                grade: '',
                class_enable: ''
            },
            academy_name: '',
            cityList:[],
            schoolList: [],
            academyList: [],
            gradeList:[{
                grade: 1,
                label: '大一'
            },{
                grade: 2,
                label: '大二'
            },{
                grade: 3,
                label: '大三'
            },{
                grade: 4,
                label: '大四'
            },{
                grade: 5,
                label: '大五'
            }],
            activeStatus: [{
                value: 0,
                label: '全部'
            },{
                value: 1,
                label: '未激活'
            },{
                value: 2,
                label: '已激活'
            }],
            leader_no: '',
            studentList: [],
            authResult: {},
            tasks: [],
            tableData: [],
            tableData1: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            transformIds: []
        };
    },
    created() {
        this.authResult = this.$route.meta.authResult;
        this.$store.commit('SET_HEADER', [
            {
                title: "任务反馈系统",
            },
            {
                title: "任务列表"
            },
            {
                title: "精准投放"
            }
        ]);
        this.fnGetTaskLimitStatus();
        this.getTableData(1);
        this.getCityList();
    },
    methods: {
        // 删除所选
        fnRemoveFromSelectedList() {
            let selected = [];

            for(let i = 0; i < this.multipleSelection.length; i++) {
                selected.push(this.multipleSelection[i].class_no);
            }

            this.$post('/task/task%5Cdelete_select_class', {
                task_no: this.$route.query.task_no,
                class_nos: selected.toString()
            }, resp => {
                if (resp.code == 1) {
                    this.$message({
                        type: 'success',
                        message: resp.msg
                    });
                    this.multipleSelection = [];
                    this.checkedAll = false;
                    this.getTableData(1);
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        // 加入所选
        fnAddToSelectedList() {
            let selected = [];

            for(let i = 0; i < this.multipleSelection.length; i++) {
                selected.push(this.multipleSelection[i].class_no);
            }

            this.$post('/task/task%5Cselect_class', {
                task_no: this.$route.query.task_no,
                class_nos: selected.toString()
            }, resp => {
                if (resp.code == 1) {
                    this.$message({
                        type: 'success',
                        message: resp.msg
                    });
                    this.multipleSelection = [];
                    this.checkedAll = false;
                    this.getTableData(1);
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        fnChangeSelectStatus() {
            this.checkIndeterminate = false;
            this.checkedAll = false;
            this.multipleSelection = [];
            this.getTableData(1);
        },
        fnSelect(selection) {
            if(selection.length == this.tableData1.length) {
                this.checkedAll = true;
            }else{
                this.checkedAll = false;
            }

            // 改变全选checkbox 的显示状态
            if(selection.length > 0 && selection.length < this.tableData1.length) {
                this.checkIndeterminate = true;
            }else{
                this.checkIndeterminate = false;
            }
        },
        fnSelectAllTop(selection) {
            console.log(this.$refs.multipleTable);
            if(selection.length > 0) {
                this.checkedAll = true;
            }else{
                this.checkedAll = false;
            }

            // 改变全选checkbox 的显示状态
            if(selection.length > 0 && selection.length < this.tableData.length) {
                this.checkIndeterminate = true;
            }else{
                this.checkIndeterminate = false;
            }
        },
        /**
         * 全选按钮状态更改
         */
        fnChangeChecked() {
            if(this.checkedAll){
                this.toggleSelection(1);
            }else{
                this.toggleSelection();
            }
        },
        toggleSelection(rows) {
            if (rows) {
                this.$refs.multipleTable.toggleAllSelection();
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        /**
         * 搜索
         */
        fnSearch() {
            this.getTableData(1);
        },
        /**
         * 更改城市
         */
        fnCityChange() {
            this.filterData1.school_id = '';
            this.filterData1.academy_no = '';
            this.getSchoolList();
            this.getTableData(1);
        },
        /**
         * 更改学校
         */
        fnSchoolChange() {

            // for(let i = 0; i < this.schoolList.length; i++) {
            //     if(this.school_id == this.schoolList[i].school_id) {
            //         this.school_name = this.schoolList[i].name;
            //     }
            // }
            this.filterData1.academy_no = '';
            this.getAcademyList();
            this.getTableData(1);
        },
        /**
         * 更改学院
         */
        fnAcademyChange() {
            // for(let i = 0; i < this.academyList.length; i++) {
            //     if(this.academy_no == this.academyList[i].academy_no) {
            //         this.academy_name = this.academyList[i].name;
            //     }
            // }
            this.getTableData(1);
        },
        /**
         * 获取城市列表
         */
        getCityList() {
            this.$post('/task/base_city%5Cget_simple_list', {
            }, resp => {
                if (resp.code == 1) {
                    this.cityList = resp.data.list;
                }
            });
        },
        /**
         * 获取学校列表
         */
        getSchoolList() {
            this.$post('/task/base_school%5Cget_simple_list', {
                city_id: this.filterData1.city_id
            }, resp => {
                if (resp.code == 1) {
                    this.schoolList = resp.data.list;
                }
            });
        },
        /**
         * 获取学院列表
         */
        getAcademyList() {
            this.$post('/task/base_academy%5Cget_simple_list', {
                school_id: this.filterData1.school_id
            }, resp => {
                if (resp.code == 1) {
                    this.academyList = resp.data.list;
                }
            });
        },
        /**
         * 获取表格数据
         */
        getTableData(page) {
            // 已选 列表
            this.checkIndeterminate = false;
            this.checkedAll = false;
            this.multipleSelection = [];
            if(this.classSelected == 1) {
                this.fnGetTableDataSelected(page);
            }else{
                this.fnGetTableDataUnSelected(page);
            }
        },

        /**
         * 列表渲染辅助函数：列表项为空处理
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        },
        /**
         * 跳转页面
         */
        handleRoute(name, query) {
            let obj = {
                name
            };

            if (query) {
                obj.query = query;
            }

            this.$router.push(obj);
        },
        /**
         * 设置班级状态
         */
        fnSetClassOpenStatus() {
            this.$post('/task/task%5Cset_class_limit', {
                task_no: this.$route.query.task_no,
                class_limit: this.class_limit ? 0 : 1
            }, resp => {
                if (resp.code == 1) {
                    this.$message({
                        type: 'success',
                        message: resp.msg
                    });
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        /**
         * 获取未选班级列表
         */
        fnGetTableDataUnSelected(page) {
            this.$post('/task/task%5Cget_not_select_class_list', {
                task_no: this.$route.query.task_no,
                class_enable: this.filterData1.class_enable,
                city_id: this.filterData1.city_id,
                school_id: this.filterData1.school_id,
                academy_no: this.filterData1.academy_no,
                grade: this.filterData1.grade,
                page_id: page || this.page.now_page
            }, resp => {
                if (resp.code == 1) {
                    this.tableData1 = resp.data.list;
                    this.page = resp.data.page;
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        /**
         * 获取已选班级列表
         */
        fnGetTableDataSelected(page) {
            this.$post('/task/task%5Cget_select_class_list', {
                task_no: this.$route.query.task_no,
                page_id: page || this.page.now_page
            }, resp => {
                if (resp.code == 1) {
                    this.tableData1 = resp.data.list;
                    this.page = resp.data.page;
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        /**
         * 重置
         */
        fnReset() {
            this.filterData1 = {
                city_id: '',
                school_id: '',
                academy_no: '',
                grade: '',
                class_enable: ''
            };
            this.getTableData(1);
        },
        /**
         * 上架发送消息
         */
        fnSendMsgToSelectedClass() {
            this.$post('/task/task%5Csend_message_to_select_class', {
                task_no: this.$route.query.task_no
            }, resp => {
                if (resp.code == 1) {
                    this.$message({
                        type: 'success',
                        message: resp.msg
                    });
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        /**
         * 获取任务限制状态
         */
        fnGetTaskLimitStatus() {
            this.$post('/task/task%5Cget_class_limit', {
                task_no: this.$route.query.task_no
            }, resp => {
                if (resp.code == 1) {
                    this.class_limit = resp.data.class_limit == 0 ? true : false;
                    console.log(this.class_limit);
                }
            });
        },
    }
};
</script>

<style lang="less" scoped>
.container {
    display: flex;
    flex-direction: column;
    width: 100%;
}
.operation-wrapper {
    display: flex;
    flex-direction: row-reverse;
    padding: 12px 23px;
    background-color: #fff;

    .el-button {
        margin-left: 10px;
    }
}
.content-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 15px;
    // overflow: hidden;
    // overflow-y: auto;
    margin-top: 0;
}
.search-wrapper {
    padding: 15px 15px 0 15px;
    background-color: #fff;
}
.tab-switch-wrapper{
    padding: 15px;
    padding-bottom: 0;
    background-color: #fff;

}
.set-status-wrapper{
    height: 50px;
    line-height: 50px;
    padding: 0 15px;
}
.filter-item {
    margin-bottom: 15px;
    .el-form-item {
        margin-bottom: 0;
    }
    .el-autocomplete {
        width: 100%;
    }
}
.table-wrapper {
    flex: 1;
    background-color: #fff;
    overflow: hidden;
    margin: 0 -10px;
    .el-button {
        padding: 0;
    }

    .btn-delete {
        color: #fd4d32;
        &.is-disabled {
            color: #c0c4cc;
            cursor: not-allowed;
        }
    }
    .class-operate{
        .el-button+.el-button{
            margin-left: 0;
        }
    }
}
.class-status {
    position: relative;
    display: inline-block;
    padding-left: 9px;
    &::before {
        content: '';
        position: absolute;
        top: 50%;
        left: 0;
        transform: translate(0, -50%);
        width: 3px;
        height: 3px;
        border-radius: 50%;
    }
    &.wait-submit,
    &.wait-audit {
        &::before {
            background-color: #F5A623;
        }
    }
    &.deny {
        &::before {
            background-color: #FF0019;
        }
    }
    &.completed {
        &::before {
            background-color: #00C800;
        }
    }
}
.pagination-wrapper {
    .select-all{
        display: inline-block;
        padding-left: 14px;
    }
    .del-btn{
        margin-left: 20px;
    }
    padding-top: 10px;
    .el-pagination {
        float: right;
        padding: 0;
        background-color: transparent;
    }
}
.change-desc{
    font-size:14px;
    color:rgba(0,0,0,0.85);
    margin-bottom: 15px;
}
.change-select{
    width: 100%;
}
</style>
<style lang="less">
.tab-switch-wrapper{
    .el-radio-button__inner{
        padding: 8px 30px!important;

    }
    .el-radio-button__orig-radio:checked+.el-radio-button__inner {
        color: #409EFF;
        background-color: #fff;
        border-color: #409EFF;
        box-shadow: -1px 0 0 0 #409EFF;
    }
}
</style>
